{include file="public/layout" /}
<body class="laytp-container">
<div id="search-form">
	<div class="layui-layer-title">搜索</div>
	<div class="layui-card" id="search-form">
    <div class="layui-card-body">
        <form class="layui-form layui-row layui-col-space16 layui-search-admin">
		<div class="layui-inline">
			<label class="layui-form-label">商家ID</label>
			<div class="layui-input-inline">
				<input type="text" name="id" value="" placeholder="商家ID" class="layui-input" lay-affix="clear">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">商家名称</label>
			<div class="layui-input-inline">
				<input type="text" name="name" value="" placeholder="商家名称" class="layui-input" lay-affix="clear">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label"></label>
			<button class="layui-btn" lay-submit lay-filter="demo-table-search">搜索</button>
			<button type="reset" class="layui-btn layui-btn-primary">清空</button>
		</div>
	</form>
</div>
<div class="layui-card">
	<div class="layui-card-body">
		<table id="goods" lay-filter="test"></table>
	</div>
</div>
<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container">
		<button class="layui-btn layui-btn-sm" onclick="reload()">
			刷新数据
		</button>
	</div>
</script>

<script>
	var tableIns
	layui.use(function(){
		var table = layui.table;
		var dropdown = layui.dropdown;
		var laydate = layui.laydate;
		var form = layui.form;

		// 创建渲染实例
		tableIns = table.render({
			elem: '#goods',
			url:'{:url("index")}', // 此处为静态模拟数据，实际使用时需换成真实接口
			toolbar: '#toolbarDemo',
			defaultToolbar: ['filter', 'exports', 'print', {
				title: '提示',
				layEvent: 'LAYTABLE_TIPS',
				icon: 'layui-icon-tips'
			}],
			height: 'full-35', // 最大高度减去其他容器已占有的高度差
			css: [ // 重设当前表格样式
				'.layui-table-tool-temp{padding-right: 145px;}'
			].join(''),
			cellMinWidth: 80,
			totalRow: false, // 开启合计行
			page: true,
			pagebar: '#test-pagebar', // 分页栏模板
			cols: [[
				{type: 'checkbox', fixed: 'left'},
				{field:'id', fixed: 'left', width:80, title: 'ID', sort: true},
				{field:'name', width:150, title: '商家名称'},
				{field:'desc', width:150, title: '商家描述'},
				{field:'nickname', width:80, title: '联系人'},
				{field:'mobile', width:80, title: '联系电话'},
				{field: 'logo', title: '商家logo',templet: function(d){
						return '<img src="'+d.logo+'" width="50" height="50" />'
					}},
				{field:'w_time', title:'添加时间'},
				// {field: 'reg_time', title: '注册时间',sort: true,templet:function(d){ return date('Y-m-d H:i',d.reg_time)}},

				{field: 'operation', title: '操作',templet: '<div><button class="layui-btn layui-btn-sm" onclick="choosesupply(\'{{d.id}}\')">选择</button></div>',align:'center'}
			]],
			error: function(res, msg){
				console.log(res, msg)
			}
		});

		// 触发单元格工具事件
		table.on('tool(test)', function(obj){ // 双击 toolDouble
			var data = obj.data; // 获得当前行数据
			// console.log(obj)
			if(obj.event === 'edit'){
				layer.open({
					title: '编辑 - id:'+ data.id,
					type: 2,
					shadeClose: true,
					area: ['100%','100%'],
					scrollbar:false,
					content: '{:url("edit")}/id/'+data.id
				});
			} else if(obj.event === 'more'){
				// 更多 - 下拉菜单
				dropdown.render({
					elem: this, // 触发事件的 DOM 对象
					show: true, // 外部事件触发即显示
					data: [{
						title: '查看',
						id: 'edit'
					},{
						title: '删除',
						id: 'del'
					}],
					click: function(menudata){
						if(menudata.id === 'edit'){
							layer.msg('查看操作，当前行 ID:'+ data.goods_id);
						} else if(menudata.id === 'del'){
							layer.confirm('真的删除行 [id: '+ data.goods_id +'] 么', function(index){
								$.ajax({
									type: "POST",
									url: "{:url('delete')}",
									data: {goods_ids: data.goods_id},
									success: function(res){
										console.log(res);
										if(res.status){
											obj.del(); // 删除对应行（tr）的DOM结构
											layer.close(index);
										}else{
											layer.msg(res.msg);
										}
										setTimeout(function(){
											parent.layer.closeAll();
											parent.tableIns.reload()
										},1000)
									}
								});

								// 向服务端发送删除指令
							});
						}
					},
					align: 'right', // 右对齐弹出
					style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' // 设置额外样式
				})
			}
		});

		laydate.render({
			elem: '.demo-table-search-date'
		});
		// 搜索提交
		form.on('submit(demo-table-search)', function(data){
			var field = data.field; // 获得表单字段
			// 执行搜索重载
			table.reload('test', {
				page: {
					curr: 1 // 重新从第 1 页开始
				},
				where: field // 搜索的字段
			});
			//layer.msg('搜索成功<br>此处为静态模拟数据，实际使用时换成真实接口即可');
			return false; // 阻止默认 form 跳转
		});
	});
	function add(){
		layer.open({
			title: '添加',
			type: 2,
			shadeClose: true,
			area: ['100%','100%'],
			scrollbar:false,
			content: '{:url("edit")}'
		});
	}
	function reload(){
		layui.table.reload('test', {
			where: {
				abc: 123,
				test: '新的 test1'
			},
			// scrollPos: 'fixed',  // 保持滚动条位置不变 - v2.7.3 新增
			// height: 2000, // 测试无效参数（即与数据无关的参数设置无效，此处以 height 设置无效为例）
			//url: '404',
			//page: {curr: 1, limit: 30} // 重新指向分页
		}, true);
	}
	function del(){
		var checkStatus = layui.table.checkStatus('goods');
		var data = checkStatus.data;
		var goods_ids = [];
		$.each(data, function(index, value){
			//goods_ids.append(value.goods_id);
			goods_ids.push(value.goods_id);
		});
		console.log(goods_ids);
		$.ajax({
			type: "POST",
			url: "{:url('delete')}",
			data: {goods_ids: goods_ids},
			success: function(res){
				console.log(res);
				if(res.status){
					layer.msg(res.msg);
					setTimeout(function(){
						tableIns.reload('test', {
							where: {
								abc: 123,
								test: '新的 test1'
							},
						}, true);
					},1000)
				}else{
					layer.msg(res.msg);
				}
				setTimeout(function(){
					parent.layer.closeAll();
					parent.tableIns.reload()
				},1000)
			}
		});
		//layer.alert(layui.util.escape(JSON.stringify(goods_ids)));
	}
	function excel(){

	}

	function choosesupply(id){
		var index = layer.load();
		$.post("{:url('choosesupply')}",{id:id},function(res){
			layer.close(index);
			window.parent.choosebusiness(res.data,"{$Request.param.args}");
			var index = parent.layer.getFrameIndex(window.name);
			parent.layer.close(index);
		});
	}
</script>
</body>
</html>